home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14290 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  59 lines

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: scope of operators
  5. Date: 29 Mar 1996 16:25:10 GMT
  6. Organization: Netcom
  7. Message-ID: <4jh2t6$scq@dfw-ixnews6.ix.netcom.com>
  8. References: <4jd23q$jp4@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: den-co11-03.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Fri Mar 29 10:25:10 AM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <4jd23q$jp4@vixen.cso.uiuc.edu>, wemccaug@prairienet.org says...
  16. >
  17. >
  18. > It is my understanding that if I declare 'f' to be a
  19. > friend of a base class, it should apply to objects of
  20. > the derived class with impunity. For instance, if 'f'
  21. > is supposed to take one argument that is an object of
  22. > the base class and I pass an object of the derived
  23. > class instead, should I not expect conformity? I am not
  24. > doing anything in the derived class object apart from
  25. > what it has inherited from the base class at all.
  26.  
  27. The following compiles fine for BC++4.5:
  28.  
  29. class A {
  30.     friend void f(A*);
  31. private:
  32.     int x;
  33. };
  34.  
  35. class B: public A
  36. {
  37. };
  38.  
  39. void f(A* a)
  40. {
  41.     a->x = 1;
  42. }
  43.  
  44. void g()
  45. {
  46.     A a;
  47.     B b;
  48.  
  49.     f(&a);
  50.     f(&b);
  51. }
  52.  
  53.  
  54. You must make sure that A is a *public* base of B, else you cannot
  55. implicitly case *B to *A.
  56.  
  57. john lilley
  58.  
  59.